<template>
	<view>
		<view class="canvasBox">
			<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
				@touchend="touchend"></canvas>
		</view>
		<view class="footer">
			<view class="footerbtn clear" @click="clear">清空</view>
			<view class="footerbtn sub" @click="finish">确定</view>
		</view>
		<!-- <view class="signBox">
			<image class="preview" :src="previewPath" mode=""></image>
		</view> -->
	</view>
</template>

<script>
	// 阻止了touchmove事件
	//在PC端运行这段代码基本上没有问题了，但在微信公众号打开或是其他移动端浏览器打开时都会出现和浏览器原生滑动事件冲突的现象，
	//例如微信公众号里写‘丨’的时候会造成浏览器页面下拉，所写笔画会出现中断严重影响用户体验，需要添加如下代码：
	// #ifdef H5
	document.body.addEventListener('touchmove', function(e) {
		e.preventDefault();
	}, {
		passive: false
	});
	// #endif
	var x = 20;
	var y = 20;
	export default {
		name: 'autograph',
		data() {
			return {
				ctx: '', //绘图图像
				points: [], //路径点集合 
				isEnd: false, // 是否结束签名
				previewPath: ""
			}
		},
		created() {
			var that = this
			//获取系统信息
			uni.getSystemInfo({
				success: function(res) {
					that.ctx = uni.createCanvasContext("mycanvas", that); //创建绘图对象
					that.width = res.windowWidth
					that.height = res.windowHeight

					//设置画笔样式
					that.ctx.lineWidth = 3;
					that.ctx.lineCap = "round"
					that.ctx.lineJoin = "round"
					that.ctx.fillStyle = "#ffffff"
					that.ctx.fillRect(0, 0, res.windowWidth, res.windowHeight)
				}
			});
		},
		methods: {
			//触摸开始，获取到起点
			touchstart(e) {
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};
				this.points.push(startPoint);
				//每次触摸开始，开启新的路径
				this.ctx.beginPath();
			},

			//触摸移动，获取到路径点
			touchmove(e) {
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}

			},

			// 触摸结束，将未绘制的点清空防止对后续路径产生干扰
			touchend() {
				this.points = [];
				this.isEnd = true
			},

			/* ***********************************************
			#   绘制笔迹
			#	1.为保证笔迹实时显示，必须在移动的同时绘制笔迹
			#	2.为保证笔迹连续，每次从路径集合中区两个点作为起点（moveTo）和终点(lineTo)
			#	3.将上一次的终点作为下一次绘制的起点（即清除第一个点）
			************************************************ */
			draw() {
				let point1 = this.points[0]
				let point2 = this.points[1]
				this.points.shift()
				this.ctx.moveTo(point1.X, point1.Y)
				this.ctx.lineTo(point2.X, point2.Y)
				this.ctx.stroke()
				this.ctx.draw(true)
			},

			//清空画布
			clear() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.ctx.clearRect(0, 0, canvasw, canvash);
						that.ctx.draw(true);
					},
				})
			},

			//完成绘画并保存到本地
			finish() {
				let that = this
				if (!this.isEnd) {
					uni.showToast({
						title: '请先完成签名',
						icon: "none",
						duration: 1500,
						mask: true
					})
					return
				}
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: (res) => {
						console.log(res)
						that.$emit('signDone', res.tempFilePath)
						this.previewPath = res.tempFilePath;
						//    let path = res.tempFilePath;
						// uni.saveImageToPhotosAlbum({
						// 	filePath:path,
						// })
					},
					fail: (err) => {
						console.log(err);
					}
				}, this)
			}
		},
	}
</script>

<style lang="less">
	.preview {
		width: 100%;
	}
	.title {
		height: 50upx;
		line-height: 50upx;
		font-size: 16px;
	}
	
	.canvasBox {
		width: 100%;
		height: calc(100vh - 100upx - constant(safe-area-inset-bottom));
		height: calc(100vh - 100upx - env(safe-area-inset-bottom));
		// height: calc(100vh - 100upx - );
		.mycanvas {
			width: 100%;
			height: 100%;
			background-color: #fff;
		}
	}

	.footer {
		font-size: 16px;
		height: 100upx;
		display: flex;
		justify-content: space-around;
		align-items: center;
		background-color: #fff;
		border-top: 1px solid #eee;
		.footerbtn {
			width: 50%;
			height: 100upx;
			line-height: 100upx;
			text-align: center;
		}
		.clear {
			color: #ff0000;
		}
		.sub {
			color: #fff;
			background-color: #007AF4;
		}
	}

	.left,
	.right {
		line-height: 100upx;
		height: 100upx;
		width: 250upx;
		text-align: center;
		font-weight: bold;
		color: white;
		border-radius: 5upx;
	}

	.left {
		background: #007AFF;
	}

	.right {
		background: orange;
	}
</style>
